package com.haitaoit.chengquan.module.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.home.model.SearchListModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by Administrator on 2017/1/18.
 */
public class SearchListAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<SearchListModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public SearchListAdapter(Context context) {
        this.context = context;
    }

    public List<SearchListModel> getList() {
        return list;
    }

    public void setList(List<SearchListModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.textview_search, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // type:分类1配件商城2论坛3服务4需求
        if (list.get(position).getType().equals("1")) {
            holder.text.setText("【配件商城】" + list.get(position).getTitle());
        } else if (list.get(position).getType().equals("2")) {
            holder.text.setText("【论坛中心】" + list.get(position).getTitle());
        } else if (list.get(position).getType().equals("3")) {
            holder.text.setText("【服务】" + list.get(position).getTitle());
        } else if (list.get(position).getType().equals("4")) {
            holder.text.setText("【需求】" + list.get(position).getTitle());
        }
        holder.text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchListAdapter.this.backCall.deal(R.id.text, position);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.text)
        TextView text;
    }
}
